#pragma once
#include<pthread.h>
#include<iostream>

namespace mutexmodule
{
    class mutex
    {
    public:
        mutex(){
            pthread_mutex_init(&_mutex,nullptr);
        }
        void lock()
        {
            pthread_mutex_lock(&_mutex);
        }
        pthread_mutex_t* addr()
        {
            return &_mutex;
        }
        void unlock()
        {
            pthread_mutex_unlock(&_mutex);
        }
        ~mutex(){
            pthread_mutex_destroy(&_mutex);
        }
    private:
        pthread_mutex_t _mutex;
    };

    class lockgroud
    {
    public:
        lockgroud(mutex& lock):_mutex(lock){
            _mutex.lock();
        }
        ~lockgroud(){
            _mutex.unlock();
            // std::cout<<"开锁成功"<<std::endl;
        }
    private:
        mutex& _mutex;
    };
}